/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.se2.meteocal.control;

import it.polimi.se2.meteocal.entity.Event;
import it.polimi.se2.meteocal.entity.Participation;
import it.polimi.se2.meteocal.entity.Weather;
import java.util.Date;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.SimpleEmail;

/**
 *
 * @author Ugo
 */
public class EmailSender {
    private final int port = 465;
    private final String hostname="smtp.googlemail.com";
    private final String from= "0meteocal0@gmail.com";
    //PASSWORD DA AGGIORNARE QUI
    private final DefaultAuthenticator user=new DefaultAuthenticator("0meteocal0@gmail.com", "g9xmb12345");
  
            
    public EmailSender(){
        
    }
    
    public void sendEmailInvitation(List<Participation> part,Event event){
        boolean error=false;
        for(Participation p:part){
            Email email = new SimpleEmail();
            email.setHostName(hostname);
            email.setSmtpPort(port);
            email.setAuthenticator(user);
            email.setSSLOnConnect(true);
            email.setSubject("Meteocal - New invitation from:  " + event.getCalendar().getUser().getEmail() + " for: " + event.getTitle());
            try{
                email.setFrom(from);
                email.setMsg("Hello "+ p.getUser().getName() + "!\nYou have a new invitation from: " + event.getCalendar().getUser().getEmail() + "( " + event.getCalendar().getUser().getName() + " " + event.getCalendar().getUser().getSurname() + ")\nFollowing event details:\n\nTitle: "+ event.getTitle() + "\nLocation: " + event.getLocation() + "\nDate: " + event.getData() + "\nBegin time: " + event.getBeginn() + "\nEnd time: " + event.getEndd() + "\nDescription: "+ event.getActivity() + "\nOutdoor: " + event.isOutdoor() + "\nPrivate: " + event.isPrivatee());
                email.addTo(p.getUser().getEmail());
                email.send();
            }
            catch(Exception e){   
                error=true;
                message("Error occured during email sending to: " + p.getUser().getEmail(), FacesMessage.SEVERITY_ERROR);
            }
        }
         if(error==false)
              message("Email sending completed", FacesMessage.SEVERITY_INFO);
    }
    
    public void sendEmailUpdate(List<Participation> part, Event event){
          boolean error=false;
          for(Participation p:part){
            Email email = new SimpleEmail();
            email.setHostName(hostname);
            email.setSmtpPort(port);
            email.setAuthenticator(user);
            email.setSSLOnConnect(true);
            email.setSubject("Meteocal - Event updated from:  " + event.getCalendar().getUser().getEmail() + " for: " + event.getTitle());
            try{
                email.setFrom(from);
                email.setMsg("Hello "+ p.getUser().getName() + "!\nEvent updated from: " + event.getCalendar().getUser().getEmail() + "( " + event.getCalendar().getUser().getName() + " " + event.getCalendar().getUser().getSurname() + ")\nFollowing event updated details:\n\nTitle: "+ event.getTitle() + "\nLocation: " + event.getLocation() + "\nDate: " + event.getData() + "\nBegin time: " + event.getBeginn() + "\nEnd time: " + event.getEndd() + "\nDescription: "+ event.getActivity() + "\nOutdoor: " + event.isOutdoor() + "\nPrivate: " + event.isPrivatee());
                email.addTo(p.getUser().getEmail());
                email.send();
            }
            catch(Exception e){   
                error=true;
                message("Error occured during email sending to: " + p.getUser().getEmail(), FacesMessage.SEVERITY_ERROR);
            }
        }
        if(error==false)
           message("Email sending completed", FacesMessage.SEVERITY_INFO);
    }
    
     public void sendEmail1daynotification(List<Participation> part, Event event,Weather weather){
          for(Participation p:part){
            Email email = new SimpleEmail();
            email.setHostName(hostname);
            email.setSmtpPort(port);
            email.setAuthenticator(user);
            email.setSSLOnConnect(true);
            email.setSubject("Meteocal - Bad weather for: "  + event.getTitle());
            try{
                email.setFrom(from);
                email.setMsg("Hello "+ p.getUser().getName() + "!\n"+ weather.getDescription() + " for Event: " + event.getTitle() + " in: "  + event.getLocation() + " on: " + event.getData() + "\nFollowing event details:\nTitle: "+ event.getTitle() + "\nLocation: " + event.getLocation() + "\nDate: " + event.getData() + "\nBegin time: " + event.getBeginn() + "\nEnd time: " + event.getEndd() + "\nDescription: "+ event.getActivity() + "\nOutdoor: " + event.isOutdoor() + "\nPrivate: " + event.isPrivatee());
                email.addTo(p.getUser().getEmail());
                email.send();
            }
            catch(Exception e){   
               
            }
        }
    }
     
      public void sendEmail3daysNotification(Event event,Weather weather,Date newdate){
         
          Email email = new SimpleEmail();
          email.setHostName(hostname);
          email.setSmtpPort(port);
          email.setAuthenticator(user);
          email.setSSLOnConnect(true);
          email.setSubject("Meteocal - Recommended postponement due to bad weather for: " + event.getTitle());
          try{
                email.setFrom(from);
                email.setMsg("Hello "+ event.getCalendar().getUser().getName() + "\nRecommended postponement due to bad weather ( "+ weather.getDescription() + " ) for Event: " + event.getTitle() + " in: "  + event.getLocation() + " from: " + event.getData() + " to: " + newdate  + "\nFollowing event details:\nTitle: "+ event.getTitle() + "\nLocation: " + event.getLocation() + "\nDate: " + event.getData() + "\nBegin time: " + event.getBeginn() + "\nEnd time: " + event.getEndd() + "\nDescription: "+ event.getActivity() + "\nOutdoor: " + event.isOutdoor() + "\nPrivate: " + event.isPrivatee());
                email.addTo(event.getCalendar().getUser().getEmail());
                email.send();
          }
          catch(Exception e){   
               
            }
        
       
    }
    
    private void message(String string, FacesMessage.Severity  type){
        FacesContext context = FacesContext.getCurrentInstance(); 
        context.addMessage(null, new FacesMessage(type,"", string));
    }
    
}


